package com.simga.library.adapter.recyclerview;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.simga.library.utils.LogUtil;

import butterknife.ButterKnife;

public abstract class MutiRecyclerAdapter<T> extends RecyclerView.Adapter<BaseViewHolder> {

    protected List<T> mData = new ArrayList<>();
    protected int layoutId;
    protected OnItemListener itemListener;
    protected Context mContext;
    protected View view;

    public MutiRecyclerAdapter(List<T> data, int layoutId) {
        this.mData = data;
        this.layoutId = layoutId;
    }
    public MutiRecyclerAdapter(int layoutId) {
        this.layoutId = layoutId;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (mContext == null) mContext = parent.getContext();
        view = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
        return getViewHolder();
    }

    @Override
    public void onBindViewHolder(@NonNull final BaseViewHolder holder, final int position) {
        if (itemListener != null) {
            holder.setOnItemClickListener(itemListener);
        }
        holder.mData = mData;
        holder.mContext = mContext;
        if (mData.size() > 0 && mData.size() > position) {
            holder.build(mData.get(position), position);
        }
    }
    @Override
    public int getItemCount() {
        return mData.size();
    }

    protected abstract BaseViewHolder<T> getViewHolder();


    public void setOnItemClickListener(OnItemListener itemClickListener) {
        this.itemListener = itemClickListener;
    }
}
